package com.example.myyunpan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myyunpan.entity.constants.Constants;
import com.example.myyunpan.entity.domain.FileShare;
import com.example.myyunpan.entity.dto.SessionShareDto;
import com.example.myyunpan.entity.enums.PageSize;
import com.example.myyunpan.entity.enums.ResponseCodeEnum;
import com.example.myyunpan.entity.enums.ShareValidTypeEnums;
import com.example.myyunpan.entity.query.FileShareQuery;
import com.example.myyunpan.entity.query.SimplePage;
import com.example.myyunpan.entity.vo.PaginationResultVO;
import com.example.myyunpan.exception.BusinessException;
import com.example.myyunpan.mapper.FileShareMapper;
import com.example.myyunpan.service.FileShareService;
import com.example.myyunpan.utils.DateUtil;
import com.example.myyunpan.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author 111
* @description 针对表【file_share(分享信息)】的数据库操作Service实现
* @createDate 2024-10-23 11:41:27
*/
@Service
public class FileShareServiceImpl extends ServiceImpl<FileShareMapper, FileShare>
implements FileShareService {

    @Autowired
    private FileShareMapper fileShareMapper;

    @Override
    public PaginationResultVO findListByPage(FileShareQuery query) {
        int count=fileShareMapper.findCountByParam(query);
        int pageSize = query.getPageSize() == null ? PageSize.SIZE15.getSize() : query.getPageSize();

        SimplePage page = new SimplePage(query.getPageNo(), count, pageSize);
        query.setSimplePage(page);
        List<FileShare> list = fileShareMapper.findListByParam(query);
        PaginationResultVO<FileShare> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
        return result;
    }

    @Override
    public void saveShare(FileShare share) {
        ShareValidTypeEnums typeEnum = ShareValidTypeEnums.getByType(share.getValidType());
        if (null == typeEnum) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        if (typeEnum != ShareValidTypeEnums.FOREVER) {
            share.setExpireTime(DateUtil.getAfterDate(typeEnum.getDays()));
        }
        Date curDate = new Date();
        share.setShareTime(curDate);
        if (StringTools.isEmpty(share.getCode())) {
            share.setCode(StringTools.getRandomString(Constants.LENGTH_5));
        }
        share.setShareId(StringTools.getRandomString(Constants.LENGTH_20));
        fileShareMapper.insert(share);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFileShareBatch(String userId, String[] shareArr) {
        Integer count = fileShareMapper.deleteFileShareBatch(shareArr, userId);
        if (count <= 0) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
    }

    @Override
    public FileShare getFileShareByShareId(String shareId) {
        FileShare fileShare = fileShareMapper.selectByShareId(shareId);
        return fileShare;
    }

    @Override
    public SessionShareDto checkShareCode(String shareId, String code) {
        FileShare share = fileShareMapper.selectByShareId(shareId);
        if (null == share || (share.getExpireTime() != null && new Date().after(share.getExpireTime()))) {
            throw new BusinessException(ResponseCodeEnum.CODE_902);
        }
        if (!share.getCode().equals(code)) {
            throw new BusinessException("提取码错误");
        }

        //更新浏览次数
        fileShareMapper.updateShareShowCount(shareId);
        SessionShareDto shareSessionDto = new SessionShareDto();
        shareSessionDto.setShareId(shareId);
        shareSessionDto.setShareUserId(share.getUserId());
        shareSessionDto.setFileId(share.getFileId());
        shareSessionDto.setExpireTime(share.getExpireTime());
        return shareSessionDto;
    }


}
